package com.chen;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.opencv.core.Core;

/**
 * 此工具类为训练而准备 此类的作用只是为一堆图片添加编号
 *
 * @author scorpius
 */
public class PathProcessing {

    static int count = 1;
    static Map<String, Integer> pathMap = new HashMap<String, Integer>();

    /**
     * 编号添加处理递归方法
     *
     * @param path
     */
    public static void processing(String path) {
        if (path.isEmpty()) {
            return;
        }
        File file = new File(path);
        if (!file.exists()) {
            return;
        }
        if (file.isDirectory()) {
            String[] list = file.list();
            for (String p : list) {
                processing(path + "/" + p);
            }
            count++;
        }
        if (file.isFile()) {
            pathMap.put(path, count);
        }
    }

    public static void main(String[] args) throws IOException {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        String path = "/home/scorpius/Pictures/orl_faces";
        processing(path);
        Object[] toArray = pathMap.keySet().toArray();
        Arrays.sort(toArray);
        File file = new File(path + "/images.txt");
        file.createNewFile();
        try (FileWriter fw = new FileWriter(file)) {
            for (Object obj : toArray) {
                Integer get = pathMap.get((String) obj);
                fw.write((String) obj + "    " + get + "\n");
                fw.flush();
            }
        }
    }
}
